package com.ziyunying.weather.ui.screen.util

import com.qweather.sdk.response.weather.WeatherDailyResponse
import com.qweather.sdk.response.weather.WeatherNow
import com.ziyunying.weather.R
import com.ziyunying.weather.ui.screen.model.AirQualityData
import com.ziyunying.weather.ui.screen.model.AirQualityItem
import com.ziyunying.weather.ui.screen.model.ForecastItem
import java.text.SimpleDateFormat
import java.util.Calendar
import java.util.Date
import java.util.Locale

object WeatherDataConverter {

    // 已有的转换方法
    fun convertToAirQualityItems(weatherNow: WeatherNow): List<AirQualityItem> {
        // if (false) return AirQualityData

        return listOf(
            AirQualityItem(
                title = "体感温度",
                value = "${weatherNow.feelsLike}°",
                icon = R.drawable.ic_real_feel
            ),
            AirQualityItem(
                title = "风速",
                value = "${weatherNow.windSpeed}公里/小时",
                icon = R.drawable.ic_wind_qality
            ),
            AirQualityItem(
                title = "相对湿度",
                value = "${weatherNow.humidity}%",
                icon = R.drawable.ic_rain_chance
            ),
            AirQualityItem(
                title = "能见度",
                value = "${weatherNow.vis}公里",
                icon = R.drawable.ic_uv_index
            ),
            AirQualityItem(
                title = "大气压强",
                value = "${weatherNow.pressure}hPa",
                icon = R.drawable.ic_so2
            ),
            AirQualityItem(
                title = "云量",
                value = "${weatherNow.cloud}%",
                icon = R.drawable.ic_o3
            )
        )
    }

    // 新增的转换方法
    fun convertToForecastItems(weatherDailyResponse: WeatherDailyResponse?): List<ForecastItem> {
        if (weatherDailyResponse == null || weatherDailyResponse.daily.isNullOrEmpty()) {
            return emptyList()
        }

        val result = mutableListOf<ForecastItem>()
        val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.getDefault())
        val weekdayFormat = SimpleDateFormat("EE", Locale.CHINA) // 使用中文星期格式
        val dayFormat = SimpleDateFormat("MM/dd", Locale.getDefault())

        weatherDailyResponse.daily.forEachIndexed { index, daily ->
            try {
                val date = dateFormat.parse(daily.fxDate) ?: Date()
                val calendar = Calendar.getInstance()
                calendar.time = date

                val dayOfWeek = weekdayFormat.format(date)
                val dayFormatted = dayFormat.format(date)

                // 根据天气状况选择对应的图标
                val weatherIcon = getWeatherIcon(daily.iconDay.toIntOrNull() ?: 100)

                // 温度范围
                val temperature = "${daily.tempMin}°~${daily.tempMax}°"

                // 空气质量指标 - 这里使用湿度作为示例，实际项目中可能需要其他指标
                val airQuality = "${daily.humidity}%"

                // 空气质量颜色 - 根据湿度值设置不同颜色
                val airQualityColor = getAirQualityColor(daily.humidity.toIntOrNull() ?: 50)

                result.add(
                    ForecastItem(
                        image = weatherIcon,
                        dayOfWeek = dayOfWeek,
                        date = dayFormatted,
                        temperature = temperature,
                        airQuality = airQuality,
                        airQualityIndicatorColorHex = airQualityColor,
                        isSelected = index == 0 // 第一天被选中
                    )
                )
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }

        return result
    }

    // 根据天气代码获取对应的图标资源ID
    private fun getWeatherIcon(iconCode: Int): Int {
        return when (iconCode) {
            100, 150 -> R.drawable.img_sun // 晴
            101, 102, 103, 151, 152, 153 -> R.drawable.img_cloudy // 多云
            104, 154 -> R.drawable.img_clouds // 阴
            in 200..299 -> R.drawable.img_thunder // 风
            in 300..399 -> R.drawable.img_rain // 雨
//            in 400..499 -> R.drawable.img_sun // 雪
            else -> R.drawable.img_sun // 默认晴天图标
        }
    }

    // 根据湿度值获取对应的颜色
    private fun getAirQualityColor(humidity: Int): String {
        return when {
            humidity < 30 -> "#FF5722" // 干燥 - 红色
            humidity < 60 -> "#4CAF50" // 适中 - 绿色
            else -> "#2196F3" // 潮湿 - 蓝色
        }
    }
}